//
//  TZConfigLibManager.m
//  OurFramework
//
//  Created by 949699582 on 2019/8/2.
//  Copyright © 2019 com.cunyin.paywaiter. All rights reserved.
//

#import "TZConfigLibManager.h"
#import "AppDelegate.h"
/**
 *    @brief    友盟相关
 */
#import <UMCommon/UMCommon.h>
#import <UMPush/UMessage.h>
#import <UserNotifications/UserNotifications.h>

static NSString *const umAPPKey = @"5a8fe4d5f29d9866a3000184";
static NSString *const umAppSecret = @"appsecret：p1widdajeqzqucxpt1vemzaegocgyf7r";

@interface TZConfigLibManager ()<UNUserNotificationCenterDelegate,UIApplicationDelegate>

@end

@implementation TZConfigLibManager

+ (TZConfigLibManager *)sharedInstance
{
    static TZConfigLibManager *sharedManager;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[TZConfigLibManager alloc] init];
    });
    
    return sharedManager;
}

// 初始化友盟配置
- (void)initUMWithLaunchOptions:(NSDictionary *)launchOptions{
    [UMConfigure setLogEnabled:YES]; // 开发调试时可在console查看友盟日志显示，发布产品必须移除。
    [UMConfigure initWithAppkey:umAPPKey channel:@"app store"];
    UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
    //type是对推送的几个参数的选择，可以选择一个或者多个。默认是三个全部打开，即：声音，弹窗，角标等
    entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert;
    
    if (@available(iOS 10.0,*)) {
        [UNUserNotificationCenter currentNotificationCenter].delegate = self;
        
        // 定制弹出View
        [self setCategoryNotification];
    }
    
    [self registerForRemoteNotifications];
    
    [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            // 用户选择了接收Push消息
        }else{
            // 用户拒绝接收Push消息
        }
    }];
}

#pragma mark - 注册远程通知
- (void)registerForRemoteNotifications {
    
    if (IOS_VERSION >= 10.0) {
        if (@available(iOS 10.0,*)) {
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//            center.delegate = self;
            center.delegate = (UIApplication *)[UIApplication sharedApplication].delegate;
            UNAuthorizationOptions options = UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert;
            [center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
                
            }];
        }
    } else {
        //iOS 10 before
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

-(void)setCategoryNotification{
    
    UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"关闭" options:UNNotificationActionOptionAuthenticationRequired ];
    UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"action2" title:@"启动app" options:UNNotificationActionOptionForeground];
    
    UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action3" title:@"红色样式" options:UNNotificationActionOptionDestructive];
    
    
    UNNotificationCategory  *category1 = [UNNotificationCategory categoryWithIdentifier:@"category1" actions:@[action1,action2,action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
    
    
    UNTextInputNotificationAction *action5 =  [UNTextInputNotificationAction  actionWithIdentifier:@"action5" title:@"" options:UNNotificationActionOptionForeground textInputButtonTitle:@"回复" textInputPlaceholder:@"写你想写的"];
    
    
    UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@"category2" actions:@[action5] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
    
    [[UNUserNotificationCenter currentNotificationCenter]setNotificationCategories:[NSSet setWithObjects:category1,category2, nil]];
}



@end
